from rest_framework.decorators import action
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from social.account.models import User
from social.gift.models import Gift
from social.gift.serializers import GiftSerializer
from social.utils.views import UpdateModelMixin, BaseGenericViewSet


class GiftViewSet(BaseGenericViewSet, ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin):
    queryset = Gift.objects.filter(type__in=[0, 1, 2])
    serializer_class = GiftSerializer
    permission_classes = (IsAuthenticated,)
    pagination_ordering = 'coin'

    @action(methods=['POST'], detail=True)
    def send_to(self, request, *args, **kwargs):
        to_user = User.objects.get(id=request.data.get('user_id'))
        gift = self.get_object()
        gift.send(request.user, to_user)
        return Response()

    def list(self, request, *args, **kwargs):
        self.paginator.page_size = 100
        return super(GiftViewSet, self).list(request, *args, **kwargs)
